class Feed::MoneyConsumedInTaobao < Feed
  
  belongs_to :called_back_by_feed, :class_name => "MoneyConsumedInTaobaoCallBack", :foreign_key => "called_back_by_feed_id"  
  
  validates_presence_of :added_money
  validates_presence_of :user_id,
    :money,
    :rmb,
    :consume_type
  validates_numericality_of :rmb, :greater_than => 0
  validates_numericality_of :added_money, :greater_than => 0
    
  def before_create
    self.version = 1    
    self.money = user.money + added_money
  end
  
  def after_create
    user.money += added_money
    user.save(false)
  end
  
  def call_back
    call_back = Feed::MoneyConsumedInTaobaoCallBack.new(
      :user => user,
      :subed_money => added_money,
      :rmb => rmb,
      :consume_type => consume_type
    )    
    call_back.save && update_attribute(:called_back_by_feed_id, call_back.id)
  end
    
  # to template
  def to_t
    ":user于:created_at_ago_in_words前在:mts_shop_in_taobao花费:rmb成功购买了:consume_type:,并获赠:added_money麦粒"
  end
end